from Tkinter import *
from Canvas import *
import sys
import random
from HomVec3 import *
from matrix import *
WIDTH  = 400 # width of canvas
HEIGHT = 400 # height of canvas

HPSIZE = 1 # double of point size (must be integer)
COLOR = "#0000FF" # blue

NOPOINTS = 1000

pointList = [] # list of points (used by Canvas.delete(...))

boundingbox={'x':None,'-x':None,'y':None,'-y':None,'z':None,'-z':None}

def quit(root=None):
    """ quit programm """
    if root==None:
        sys.exit(0)
    root._root().quit()
    root._root().destroy()

def actBBox(param):
    global boundingbox

    for ele in param.keys():
        if boundingbox['-%s'%ele] > param[ele] or not boundingbox['-%s'%ele]:
            boundingbox['-%s'%ele]=param[ele]
        if boundingbox['%s'%ele] < param[ele] or not boundingbox['%s'%ele]:
            boundingbox['%s'%ele]=param[ele]

def getPoints(fname):
    global pointList
    for line in open(fname):
        x,y,z=map(float,line.split())
        actBBox({"x":x,"y":y,"z":z})
        pointList.append([x,y,z])
        
    print "fertig"


def getMid():
  global boundingbox    
  p = HomVec3(boundingbox['-x'],boundingbox['-y'],boundingbox['-z'],1)
  q = HomVec3(boundingbox['x'],boundingbox['y'],boundingbox['z'],1)
  mid = (p+q) / 2
  
  return mid


def moveToOrigin():
    #Mittelpunkt ermitteln
    mid = getMid()
    print mid
    
    #Transformationsmatrix
    tm=Matrix([1,0,0,mid.x,0,1,0,mid.y,0,0,1,mid.z,0,0,0,1])
    
    
    
    
    print tm
    
    
def draw():
    global pointList
    """ draw points """

    for i in range(1,NOPOINTS):
        x,y = random.randint(1,WIDTH), random.randint(1,HEIGHT)
        p = can.create_oval(x-HPSIZE, y-HPSIZE, x+HPSIZE, y+HPSIZE,fill=COLOR, outline=COLOR)
        pointList.insert(0,p)
    
def rotYp():
    """ rotate counterclockwise around y axis """
    global NOPOINTS
    NOPOINTS += 100
    print "In rotYp: ", NOPOINTS 
    can.delete(*pointList)
    draw()

def rotYn():
    """ rotate clockwise around y axis """
    global NOPOINTS
    NOPOINTS -= 100
    print "In rotYn: ", NOPOINTS 
    can.delete(*pointList)
    draw()


if __name__ == "__main__":
    #check parameters
    if len(sys.argv) != 1:
       print "pointViewerTemplate.py"
       sys.exit(-1)

    # create main window
    mw = Tk()

    # create and position canvas and buttons
    cFr = Frame(mw, width=WIDTH, height=HEIGHT, relief="sunken", bd=1)
    cFr.pack(side="top")
    can = Canvas(cFr, width=WIDTH, height=HEIGHT)
    can.pack()
    bFr = Frame(mw)
    bFr.pack(side="left")
    bRotYn = Button(bFr, text="<-", command=rotYn)
    bRotYn.pack(side="left")
    bRotYp = Button(bFr, text="->", command=rotYp)
    bRotYp.pack(side="left")
    eFr = Frame(mw)
    eFr.pack(side="right")
    bExit = Button(eFr, text="Quit", command=(lambda root=mw: quit(root)))
    bExit.pack()

    # read Points
    getPoints("squirrel_points.raw")
    print boundingbox
    
    moveToOrigin()
    
    # draw points
    draw()

    # start
    #mw.mainloop()
    
